/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Persistencia;

/**
 *
 * @author marcelo
 */
import cprincipal.ambulancias;
import cprincipal.ambulanciaCompacta;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

public class AmbulanciaCompactaXmlAmbulatorio
{
    private Element root;
    private String fileLocation = "src/Archivos/ambulanciasCompactasAmbulatorio.xml";

    public AmbulanciaCompactaXmlAmbulatorio()
    {
        try
        {
            SAXBuilder builder = new SAXBuilder(false);
            Document doc = null;
            doc = builder.build(fileLocation);
            root = doc.getRootElement();
        }
        catch (JDOMException ex)
        {
            System.out.println("No se pudo iniciar la operacion por: " + ex.getMessage());
        }
        catch (IOException ex)
        {
            System.out.println("No se pudo iniciar la operacion por: " + ex.getMessage());
        }
    }

    private Element AmbulanciaCompactatoXmlElement(ambulanciaCompacta nComp)
    {
        Element ambulanciasTRA = new Element("compacta");

        Element cedulaConductor = new Element("cedulaConductor");
        cedulaConductor.setText(Integer.toString(nComp.getCedulaConductor()));
        Element cedulaParamedico = new Element("cedulaParamedico");
         cedulaParamedico.setText(Integer.toString(nComp.getCedulaParamedico()));
        Element cedulaPaciente = new Element("cedulaPaciente");
        cedulaPaciente.setText(Integer.toString(nComp.getCedulaPaciente()));
        Element  horaUsoAmbulancia = new Element ("horaUsoAmbulancia");
         horaUsoAmbulancia.setText(Integer.toString(nComp.getHoraUsoAmbulancia()));
        Element  horaUltimoRescate = new Element ("horaUltimoRescate");
          horaUltimoRescate.setText(Integer.toString(nComp.getHoraUltimoRescate()));
        Element  matricula = new Element ("matricula");
         matricula.setText(Integer.toString(nComp.getMatricula()));
          Element  infraestructura = new Element ("infraestructura");
         infraestructura.setText(nComp.getInfraestructura());
      
        ambulanciasTRA.addContent(cedulaConductor);
        ambulanciasTRA.addContent(cedulaParamedico);
        ambulanciasTRA.addContent(cedulaPaciente);
        ambulanciasTRA.addContent(horaUsoAmbulancia);
        ambulanciasTRA.addContent(horaUltimoRescate);
        ambulanciasTRA.addContent(matricula);
         ambulanciasTRA.addContent(infraestructura);
       

        return ambulanciasTRA;
    }

      private ambulanciaCompacta ambulanciaCompactaToObject(Element element) throws ParseException
      {
      ambulanciaCompacta nComp = new ambulanciaCompacta ();

                 nComp.setCedulaConductor(Integer.parseInt(element.getChildText("cedulaConductor")));
                 nComp.setCedulaParamedico(Integer.parseInt(element.getChildText("cedulaParamedico")));
                 nComp.setCedulaPaciente(Integer.parseInt(element.getChildText("cedulaPaciente")));
                 nComp.setHoraUsoAmbulancia(Integer.parseInt(element.getChildText("horaUsoAmbulancia")));
                 nComp.setHoraUltimoRescate(Integer.parseInt(element.getChildText("horaUltimoRescate")));
                 nComp.setMatricula(Integer.parseInt(element.getChildText("matricula")));
                 nComp.setInfraestructura(element.getChildText("infraestructura"));


        return nComp;
    }

         public static Element buscar(List raiz, String dato, String hola) {
        Iterator i = raiz.iterator();
        while (i.hasNext()) {
            Element e = (Element) i.next();
            if (dato.equals(e.getChild("matricula").getText()) && hola.equals(e.getChild("infraestructura").getText())  ) {
                return e;
            }
        }
        return null;
    }


    private boolean updateDocument() {
        try {
            XMLOutputter out = new XMLOutputter(org.jdom.output.Format.getPrettyFormat());
            FileOutputStream file = new FileOutputStream(fileLocation);
            out.output(root, file);
            file.flush();
            file.close();
            return true;
        } catch (Exception e) {
            System.out.println("error: " + e.getMessage());
            return false;
        }
    }

    public boolean agregarAmbulanciaCompacta(ambulanciaCompacta nComp) {
        boolean resultado = false;
        root.addContent(AmbulanciaCompactatoXmlElement(nComp));
        resultado = updateDocument();
        return resultado;
    }



 public ambulanciaCompacta buscarAmbulanciaCompacta(Integer dato, String hola) {
        Element aux = new Element("compacta");
        List Ambulancias = this.root.getChildren("compacta");

        while (aux != null) {
            aux = AmbulanciaCompactaXmlAmbulatorio.buscar(Ambulancias,  Integer.toString(dato), hola);
            if (aux != null) {
                try {
                    return ambulanciaCompactaToObject(aux);
                } catch (ParseException ex) {
                    System.out.println(ex.getMessage());
                }
            }
        }
        return null;
    }

  public boolean actualizarAmbulanciaCompacta(ambulanciaCompacta nComp) {
        boolean resultado = false;
        Element aux = new Element("compacta");
        List Ambulancias = this.root.getChildren("compacta");
        while (aux != null) {
            aux = AmbulanciaCompactaXmlAmbulatorio.buscar(Ambulancias, Integer.toString(nComp.getMatricula()),  nComp.getInfraestructura() );
            if (aux != null) {
                Ambulancias.remove(aux);
                resultado = updateDocument();
            }
        }
        agregarAmbulanciaCompacta(nComp);
        return resultado;
    }

 


     public ArrayList<ambulanciaCompacta> todasLasAmbulanciasAereas() {
        ArrayList<ambulanciaCompacta> resultado = new ArrayList<ambulanciaCompacta>();
        for (Object it : root.getChildren()) {
            Element xmlElem = (Element) it;
            try {
                resultado.add(ambulanciaCompactaToObject(xmlElem));
            } catch (ParseException ex) {
                System.out.println(ex.getMessage());
            }
        }
        return resultado;
    }
}


